package com.xiaoai.juc;

import java.util.concurrent.CyclicBarrier;

class writer extends Thread{
    public CyclicBarrier cyclicBarrier;
    public writer(CyclicBarrier cyclicBarrier) {
        this.cyclicBarrier = cyclicBarrier;
    }
    @Override
    public void run() {
        System.out.println(currentThread().getName()+"开始写入数据");
        try {
            Thread.sleep(30);//模拟消耗时间
            System.out.println(currentThread().getName()+"写入数据成功...");
            cyclicBarrier.await(); //所有线程执行完才并发执行下面的代码
            System.out.println(currentThread().getName()+"所有数据执行完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
public class Tbf_CyclicBarrier {
    public static void main(String[] args){  
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5); //初始5个线程执行完毕后才并发
        for (int i=0;i<5;i++){ //创建5个线程执行，需要最后“”一起写出
            new writer(cyclicBarrier).start();
        }
    }
    
}